package com.niuxiaofa.newlaw.Tools;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.Toast;

import com.niuxiaofa.newlaw.R;
import com.sina.weibo.sdk.api.WebpageObject;
import com.sina.weibo.sdk.api.WeiboMultiMessage;
import com.sina.weibo.sdk.auth.AuthInfo;
import com.sina.weibo.sdk.openapi.IWBAPI;
import com.sina.weibo.sdk.openapi.WBAPIFactory;
import com.tencent.connect.share.QQShare;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class ShareTool {


    public static void shareVX(Context context, String title, String content, String webUrl, int pyqqzome) {
        // 通过appId得到IWXAPI这个对象
        IWXAPI mWxApi = WXAPIFactory.createWXAPI(context, "wx096ab426e89a60a0", true);
        // 检查手机或者模拟器是否安装了微信
        if (!mWxApi.isWXAppInstalled()) {
            Toast.makeText(context, "您还没有安装微信", Toast.LENGTH_LONG).show();
            return;
        }

        // 初始化一个WXWebpageObject对象
        WXWebpageObject webpageObject = new WXWebpageObject();
        // 填写网页的url
        webpageObject.webpageUrl = webUrl;

        // 用WXWebpageObject对象初始化一个WXMediaMessage对象
        WXMediaMessage msg = new WXMediaMessage(webpageObject);
        // 填写网页标题、描述、位图
        msg.title = title;
        msg.description = content;
        // 如果没有位图，可以传null，会显示默认的图片


        // 构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        // transaction用于唯一标识一个请求（可自定义）
        req.transaction = "webpage";
        // 上文的WXMediaMessage对象
        req.message = msg;
        // SendMessageToWX.Req.WXSceneSession是分享到好友会话
        // SendMessageToWX.Req.WXSceneTimeline是分享到朋友圈

        if (pyqqzome==1) {
            req.scene = SendMessageToWX.Req.WXSceneSession;
        }else{
            req.scene = SendMessageToWX.Req.WXSceneTimeline;
        }
        // 向微信发送请求
        mWxApi.sendReq(req);
    }


    public static void shareQQ(Context context, String content, String url,int type) {
        Tencent mTencent = Tencent.createInstance("101935206", context);
        Bundle params = new Bundle();

        if(type==2){
            params.putInt(QQShare.SHARE_TO_QQ_EXT_INT,  QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN);
        }

        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        params.putString(QQShare.SHARE_TO_QQ_TITLE, "牛晓法");// 标题
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, content);// 摘要
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, url);// 内容地址


        mTencent.shareToQQ(UIUtils.getActivity(), params, new IUiListener() {
            @Override
            public void onComplete(Object o) {

            }

            @Override
            public void onError(UiError uiError) {

            }

            @Override
            public void onCancel() {

            }
        });
    }

    public static void shareWB(Context context, String content, String url) {

        AuthInfo authInfo = new AuthInfo(context, "4015625823", "", "");

        IWBAPI mWBAPI = WBAPIFactory.createWBAPI(context);
        mWBAPI.registerApp(context, authInfo);
        WeiboMultiMessage message = new WeiboMultiMessage();
        // 注册到新浪微博
        WebpageObject webpageObject = new WebpageObject();
        webpageObject.actionUrl = url;
        webpageObject.title = "牛晓法";
        webpageObject.defaultText = "牛晓法";
        message.mediaObject=webpageObject;

        Bitmap bitmap= BitmapFactory.decodeResource(context.getResources(), R.mipmap.logo);
        ByteArrayOutputStream os=null;

        try{
            os=new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG,85,os);
            webpageObject.thumbData=os.toByteArray();

        }catch (Exception E){

        }finally {
            {
                try {
                    if(os!=null){
                        os.close();
                    }
                }catch (IOException e){

                }
            }
        }



        mWBAPI.shareMessage(message,false);

    }

}
